package net.muliba.clicklockscreen

import android.app.admin.DevicePolicyManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.PowerManager
import android.support.v7.app.AppCompatActivity
import android.util.Log


class MainActivity : AppCompatActivity() {

    private lateinit var mComponentName: ComponentName
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        unlock()
//        val deviceManager = getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
//        mComponentName = ComponentName(this, LockReceiver::class.java)
//        if (deviceManager.isAdminActive(mComponentName)){
//            deviceManager.lockNow()//锁屏
//            unlock()
//        }else {
//            activeManager()
//        }
        //结束进程
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    private fun unlock() {
        try {
            val pm = getSystemService(Context.POWER_SERVICE) as PowerManager
            // 获取PowerManager.WakeLock对象，后面的参数|表示同时传入两个值，最后的是调试用的Tag
            val wakeLock  = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK or  PowerManager.ACQUIRE_CAUSES_WAKEUP, "bright")
            // 点亮屏幕
            wakeLock.acquire()

            wakeLock.release()
        } catch (e: Exception) {
            Log.e("lock screen ", "", e)
        }
    }

    private fun activeManager() {
        val intent = Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN)
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mComponentName)
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, getString(R.string.app_name))
        startActivity(intent)
    }
}
